Editable cells demo: Add new row at cursor
authorPhillip Wood <phillip.wood@dunelm.org.uk>
Sun, 22 Dec 2013 18:17:32 +0000 (18:17 +0000)
committerMatthias Clasen <mclasen@redhat.com>
Sun, 7 Dec 2014 23:53:30 +0000 (18:53 -0500)
Adding rows to the bottom of the list is confusing as you cannot see
them if the window is small so it is not apparent that anything has
happened. Fix this by adding the new row immediately below the current
row and set the cursor on the new row so it is ready to be edited.

https://bugzilla.gnome.org/show_bug.cgi?id=721939

demos/gtk-demo/editable_cells.c

index 917a84e4c3524e309330a973cb1160732a740ea5..18ff4771edfa3a2526dfaedb3f16cedbb0599035 100644 (file)
@@ -142,8 +142,11 @@ static void
 add_item (GtkWidget *button, gpointer data)
 {
   Item foo;
-  GtkTreeIter iter;
-  GtkTreeModel *model = (GtkTreeModel *)data;
+  GtkTreeIter current, iter;
+  GtkTreePath *path;
+  GtkTreeModel *model;
+  GtkTreeViewColumn *column;
+  GtkTreeView *treeview = (GtkTreeView *)data;
 
   g_return_if_fail (articles != NULL);
 
@@ -152,12 +155,26 @@ add_item (GtkWidget *button, gpointer data)
   foo.yummy = 50;
   g_array_append_vals (articles, &foo, 1);
 
-  gtk_list_store_append (GTK_LIST_STORE (model), &iter);
+  /* Insert a new row below the current one */
+  gtk_tree_view_get_cursor (treeview, &path, NULL);
+  model = gtk_tree_view_get_model (treeview);
+  gtk_tree_model_get_iter (model, &current, path);
+  gtk_tree_path_free (path);
+
+  /* Set the data for the new row */
+  gtk_list_store_insert_after (GTK_LIST_STORE (model), &iter, &current);
   gtk_list_store_set (GTK_LIST_STORE (model), &iter,
                       COLUMN_ITEM_NUMBER, foo.number,
                       COLUMN_ITEM_PRODUCT, foo.product,
                       COLUMN_ITEM_YUMMY, foo.yummy,
                       -1);
+
+  /* Move focus to the new row */
+  path = gtk_tree_model_get_path (model, &iter);
+  column = gtk_tree_view_get_column (treeview, 0);
+  gtk_tree_view_set_cursor (treeview, path, column, FALSE);
+
+  gtk_tree_path_free (path);
 }
 
 static void
@@ -368,7 +385,7 @@ do_editable_cells (GtkWidget *do_widget)
 
       button = gtk_button_new_with_label ("Add item");
       g_signal_connect (button, "clicked",
-                        G_CALLBACK (add_item), items_model);
+                        G_CALLBACK (add_item), treeview);
       gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
 
       button = gtk_button_new_with_label ("Remove item");